﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AGrandVenture
{
    class Particle : Sprite 
    {
        #region Declaration
        private Vector2 acceleration;
        private float maxSpeed;
        private int initialDuration;
        private int remainingDuration;
        private Color initialColour;
        private Color finalColour;
        #endregion

        #region Properties

        public bool PlayerShot
        {
            get;
            set;
        }

        public int ElapsedDuration
        {
            get
            {
                return initialDuration - remainingDuration;
            }
        }

        public float DurationProgress
        {
            get
            {
                return (float)ElapsedDuration / (float)initialDuration;
            }
        }

        public bool IsActive
        {
            get
            {
                return (remainingDuration > 0);
            }
        }
        #endregion

        #region Constructor
        public Particle(
            Vector2 location,
            Texture2D texture,
            Rectangle initialFrame,
            Vector2 velocity,
            float drawDepth,
            Vector2 acceleration,
            float maxSpeed,
            int duration,
            Color initialColour,
            Color finalColour,
            bool player)
            : base(location, texture, initialFrame, velocity, drawDepth)
        {
            initialDuration = duration;
            remainingDuration = duration;
            this.acceleration = acceleration;
            this.initialColour = initialColour;
            this.maxSpeed = maxSpeed;
            this.finalColour = finalColour;
            this.PlayerShot = player;
        }
        #endregion

        #region Update and Draw
        public override void Update(GameTime gameTime)
        {
            if (remainingDuration <= 0)
            {
                Expired = true;
            }

            if (!Expired)
            {
                Velocity += acceleration;
                if (Velocity.Length() > maxSpeed)
                {
                    Vector2 vel = Velocity;
                    vel.Normalize();
                    Velocity = vel * maxSpeed;
                }
                TintColor = Color.Lerp(initialColour, finalColour, DurationProgress);
                remainingDuration--;
            }

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (IsActive)
            {
                base.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
